using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class Controls_gallery : System.Web.UI.UserControl
{
    private string _picasaXML;

    private int pageSize = 8;
        
    public string PicasaFeed
    {
        get
        {
            return _picasaXML;
        }
        set
        {
            _picasaXML = value;
        }
    }

    public string currentPicasaFeed
    {
        get
        {
            return _picasaXML;
        } 
    }

    public bool LoadLibrarys
    {
        set
        {
            phLibrarys.Visible = value;
        }
    }

   
    //protected void updateValue()
    //{
    //    ViewState["PicasaFeed"] = currentPicasaFeed;
    //}

    protected void Page_Init(object sender, EventArgs e)
    {
        ViewState["PicasaFeed"] = "~/static/picasa/example/index.xml";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(_picasaXML))
        {
            try
            {
                XmlDocument album = new XmlDocument();
                album.PreserveWhitespace = false;

                String category = "example";

                if (Request.QueryString.HasKeys())
                {
                    category = Request.QueryString["cat"];
                }

                _picasaXML = "~/static/picasa/" + category + "/index.xml";

                //if (!Page.IsPostBack)
                //{
                //    if (Language.LanguageCode == "zh-HK")
                //    {
                //        ViewState["PicasaFeed"] = ((String)ViewState["PicasaFeed"]).Replace("index.xml", "index_chi.xml");
                //    }
                //    _picasaXML = (String)ViewState["PicasaFeed"];
                //    //Label1.Text = _picasaXML;
                //    //Label1.ForeColor = System.Drawing.Color.Blue;
                //    album.Load(Server.MapPath(_picasaXML));
                //}
                //else
                //{
                    if (Language.LanguageCode == "zh-HK")
                    {
                        _picasaXML = _picasaXML.Replace("index.xml", "index_chi.xml");
                    }
                    //Label1.Text = _picasaXML;
                    //Label1.ForeColor = System.Drawing.Color.Red;
                    album.Load(Server.MapPath(_picasaXML));
                //}

                xmlGallery.Document = album;
                System.Xml.Xsl.XsltArgumentList xargs = new System.Xml.Xsl.XsltArgumentList();
                string absPath = Page.ResolveClientUrl(_picasaXML.ToLower().Replace("/index.xml", ""));
                absPath = absPath.Replace("/index_chi.xml", "");

                xargs.AddParam("path", "", absPath);

                int currentPage = 1;
                if (Request.QueryString.HasKeys())
                {
                    currentPage = int.Parse(Request.QueryString["page"]);
                }

                xargs.AddParam("start", "", ((currentPage - 1) * pageSize) + 1);
                xargs.AddParam("end", "", ((currentPage - 1) * pageSize) + pageSize);
                xargs.AddParam("link", "", buildPageHTML());
                xargs.AddParam("returnLink", "", "<a href=\"product.aspx\">" + Resources.Resource.returnDesc + "</a>");
                xargs.AddParam("dummpyLink", "", "&nbsp;");
                xargs.AddParam("dummpyLink2", "", "&nbsp;");
                
                //xargs.AddParam("path", "", absPath);

                xmlGallery.TransformArgumentList = xargs;

                xmlGallery.TransformSource = "galleria.xsl";
                //updateValue();
            }
            catch (Exception ex)
            {
                lblMSG.Text = ex.Message;
                lblMSG.ForeColor = System.Drawing.Color.Red;
            }
        }
        else
        {
            lblMSG.Text = "Property 'PicasaFeed' is empty";
            lblMSG.ForeColor = System.Drawing.Color.Red;
        }

    }

    protected void Submit(object sender, EventArgs e)
    {
        //_picasaXML = "~/static/picasa/" + ((Button)sender).Text + "/index.xml";
        //updateValue();
        Response.Redirect("gallery.aspx?page=1&cat=" + ((Button)sender).Text);
    }

    protected String buildPageHTML()
    {
        String pagelink = "";
        XmlNodeList allNodes = xmlGallery.Document.SelectNodes("//album/images/image");
        int pages = allNodes.Count / pageSize;

        if (allNodes.Count % pageSize == 0)
        {
            pages -= 1;
        }

        int currentPage = int.Parse(Request.QueryString["page"]);

        if (currentPage != 1)
            pagelink += "<a href=\"gallery.aspx?page=" + (currentPage - 1).ToString() + "&cat=" + (String.IsNullOrEmpty(Request.QueryString["cat"]) ? "example" : Request.QueryString["cat"]) + "\">" + "<" + "</a>&#160;&#160;&#160;";
        else
            pagelink += "&nbsp;";

        for (int count = 1; count <= pages + 1; count++)
        {
            //pagelink += "<asp:Button ID=\"Page" + count + "\" runat=\"server\" Text=\"" + count + "\" OnClick=\"changePage\" />";
            pagelink += "<a href=\"gallery.aspx?page=" + count + "&cat=" + (String.IsNullOrEmpty(Request.QueryString["cat"]) ? "example" : Request.QueryString["cat"]) + "\">" + count + "</a>";

            if (count != pages + 1)
                pagelink += "&nbsp;.&nbsp;";
            else
                pagelink += "&nbsp;&nbsp;&nbsp;";
        }

        if (currentPage != pages + 1)
            pagelink += "<a href=\"gallery.aspx?page=" + (currentPage + 1).ToString() + "&cat=" + (String.IsNullOrEmpty(Request.QueryString["cat"]) ? "example" : Request.QueryString["cat"]) + "\">" + ">" + "</a>";
        else
            pagelink += "&nbsp;";

        return pagelink;
    }
}
